package main

import (
	"context"
	"google.golang.org/grpc"
	"grpc/map_test/proto"
	"net"
)

type CalServer struct {
}

// Add 实现加法
func (c CalServer) Add(context context.Context, request *proto.AddRequest) (*proto.Response, error) {
	return &proto.Response{Result: request.Parameter1 + request.Parameter2}, nil
}

// Sub 实现减法
func (c CalServer) Sub(context context.Context, request *proto.AddRequest) (*proto.Response, error) {
	return &proto.Response{Result: request.Parameter1 - request.Parameter2}, nil
}

func main() {
	g := grpc.NewServer()                     // 创建一个grpc服务
	proto.RegisterCalServer(g, &CalServer{})  // 注册
	listen, err := net.Listen("tcp", ":8088") // 监听
	if err != nil {
		panic("net.Listen(\"tcp\", \":8088\")" + err.Error())
	}

	err = g.Serve(listen) // 启动服务
	if err != nil {
		panic("g.Serve(listen)" + err.Error())
	}
}
